<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false" append-to-body
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="合同编号" prop="contractNo">
        <el-input v-model="dataForm.contractNo" placeholder="合同编号" disabled></el-input>
      </el-form-item>
      <el-form-item label="收支金额" prop="accountNum">
        <el-input v-model="dataForm.accountNum" placeholder="收支金额"></el-input>
      </el-form-item>
      <el-form-item label="支出类型" prop="accountType">
          <el-radio-group v-model="dataForm.accountType">
            <el-radio :label="'0'">收款</el-radio>
            <el-radio :label="'1'">支出</el-radio>
          </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="accountNote">
        <el-input v-model="dataForm.accountNote" placeholder="收支内容备注" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="添加时间" prop="accountAddDateTime">
        <el-date-picker v-model="dataForm.accountAddDateTime" type="date" value-format="yyyy-MM-dd" placeholder="添加时间"
                        style="width: 200px;"></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" >取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          contractNo: '',
          accountNote: '',
          accountNum: '',
          accountType: '',
          accountTypeName: '',
          accountAddDateTime: '',
          createDateTime: ''
        },
        dataRule: {
          contractNo: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          accountNum: [
            { required: true, message: '收支金额不能为空', trigger: 'blur' }
          ],
          accountType: [
            { required: true, message: '支出类型不能为空', trigger: 'blur' }
          ],
          accountTypeName: [
            { required: true, message: '支出类型名称不能为空', trigger: 'blur' }
          ],
          accountAddDateTime: [
            { required: true, message: '账号收支添加时间不能为空', trigger: 'blur' }
          ],
          createDateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (item) {
        this.dataForm.id = item.id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/finance/account/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contractNo = data.info.contractNo
                this.dataForm.accountNote = data.info.accountNote
                this.dataForm.accountNum = data.info.accountNum
                this.dataForm.accountType = data.info.accountType
                this.dataForm.accountTypeName = data.info.accountTypeName
                this.dataForm.accountAddDateTime = data.info.accountAddDateTime
                this.dataForm.createDateTime = data.info.createDateTime
                console.log(this.dataForm)
              }
            })
          } else {
            this.dataForm.contractNo = item.contractNo
            this.dataForm.accountAddDateTime = new Date()
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/finance/account/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'contractNo': this.dataForm.contractNo,
                'accountNote': this.dataForm.accountNote,
                'accountNum': this.dataForm.accountNum,
                'accountType': this.dataForm.accountType,
                'accountTypeName': this.dataForm.accountTypeName,
                'accountAddDateTime': this.dataForm.accountAddDateTime,
                'createDateTime': this.dataForm.createDateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
